package com.zhuoyue.web.controller;


import com.zhuoyue.biz.core.api.domain.SceneControllerBindingDO;
import com.zhuoyue.biz.core.api.dto.rep.SceneControllerBindingRepDTO;
import com.zhuoyue.biz.core.api.service.SceneControllerBindingService;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.dto.ControllerResult;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import com.zhuoyue.common.utils.Time;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;


/**
 * @author wjming
 * @ClassName: AppSceneControllerBindingController
 * @Description:场景控制器进行场景绑定
 * @Date 2021/4/17 12:20
 */
@RestController
@Scope("prototype")
@RequestMapping("/api/sceneControllerBinding")
@Validated
public class AppSceneControllerBindingController {

    @Autowired
    private SceneControllerBindingService sceneControllerBindingService;


    /**
     * 查询场景面板绑定的场景列表
     *
     * @param deviceId
     * @return
     */
    @GetMapping("/list")
    @CheckLogin
    public ControllerResult getSceneControllerBindingList(@RequestParam @NotBlank(message = "设备ID不能为空！") String deviceId) {

        List<SceneControllerBindingDO> result = sceneControllerBindingService.getSceneControllerBindingListByDeviceId(deviceId);
        List<SceneControllerBindingRepDTO> sceneControllerBindingRepDtos = new ArrayList<>();
        result.stream().forEach(list -> {
            sceneControllerBindingRepDtos.add(repDTO(list));
        });
        return ControllerResult.toReturnDataSuccess("查询成功", sceneControllerBindingRepDtos);
    }

    /**
     * 新增场景面板绑定信息
     *
     * @param deviceId     设备ID
     * @param wayNumber    控制路数
     * @param localSceneId 本地场景ID
     * @param sceneSetName 场景名称
     * @return
     */
    @PostMapping("/add")
    @CheckLogin
    public ControllerResult addSceneControllerBinding(@RequestParam @NotBlank(message = "设备ID不能为空！") String deviceId,
                                                      @RequestParam @NotNull("场景控制器路数不能为空！") Integer wayNumber,
                                                      @RequestParam @NotBlank(message = "本地场景ID不能为空！") String localSceneId,
                                                      @RequestParam @NotBlank(message = "场景设置名称不能为空！") String sceneSetName) {

        SceneControllerBindingDO scb = new SceneControllerBindingDO();
        scb.setDeviceId(deviceId);
        scb.setWayNumber(wayNumber);
        scb.setLocalSceneId(localSceneId);
        scb.setSceneSetName(sceneSetName);
        SceneControllerBindingDO sceneControllerBindingDO = sceneControllerBindingService.addSceneControllerBinding(scb);
        return ControllerResult.toReturnDataSuccess("添加成功", repDTO(sceneControllerBindingDO));
    }

    /**
     * @Description:删除场景面板绑定信息
     * @param: id
     * @return: com.zhuoyue.common.dto.ControllerResult
     * @author:wjming
     * @Date:2021/4/17 13:41
     */
    @DeleteMapping("/delete")
    @CheckLogin
    public ControllerResult deleteSceneControllerBinding(@RequestParam @NotBlank(message = "ID不能为空！") String id) {

        boolean removeById = sceneControllerBindingService.removeById(id);
        if (!removeById) {
            throw new ZyServerException(ErrorType.OBJECT_NOT_FOUND.getCode(), "场景面板绑定信息删除失败");
        }
        return ControllerResult.toReturnMessageSuccess("删除成功");
    }

    private SceneControllerBindingRepDTO repDTO(SceneControllerBindingDO sceneControllerBindingDO) {
        SceneControllerBindingRepDTO sceneControllerBindingRepDTO = new SceneControllerBindingRepDTO();
        BeanUtils.copyProperties(sceneControllerBindingDO, sceneControllerBindingRepDTO);
        sceneControllerBindingRepDTO.setCreateTime(Time.toLongMilli(sceneControllerBindingDO.getCreateTime()));
        return sceneControllerBindingRepDTO;
    }

}
